#include "client3.h"
#include "ui_client3.h"
#include "client4.h"
#include "client5.h"
#include "client6.h"
client3::client3(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::client3)
{
    ui->setupUi(this);


    setWindowTitle(tr("小翼智能家居"));
    m_serial = new QSerialPort(this);
    ui->pushButton_14->setEnabled(true);
    ui->pushButton_13->setEnabled(false);
    QPixmap img=QPixmap("G:/picture/6.jpg");
    ui->label->setPixmap(img);
    ui->label->setScaledContents(true);
}
//打开串口
void client3::on_pushButton_14_clicked()
{
    m_serial->setPortName("COM1");
    if (!m_serial->open(QIODevice::ReadWrite))
    {
        return;
    }
    m_serial->setBaudRate(9600);
    m_serial->setDataBits(QSerialPort::Data8);//数据位
    m_serial->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）

    m_serial->setParity(QSerialPort::NoParity);//奇偶校验位
    m_serial->setStopBits(QSerialPort::OneStop);//停止位
    if(m_serial->isOpen())
    {
        ui->textEdit->setText("串口打开成功\n");
        connect(m_serial,&QSerialPort::readyRead,this,&client3::messlot);
    }
    else
    {
        ui->textEdit->setText("串口打开失败\n");
    }
    ui->pushButton_14->setEnabled(false);
    ui->pushButton_13->setEnabled(true);
}
//接受数据
void client3::readData()
{
    QString temp = m_serial->readAll();
    if(!temp.isNull())
    {
        ui->textEdit->append(temp);
    }

}
//串口接受信息的处理函数
void client3::messlot()
{
    QByteArray array = m_serial->readAll();
    ui->textEdit->insertPlainText(array);
}

void client3::on_pushButton_13_clicked()
{
    m_serial->close();
    if(m_serial->isOpen())
    {
        ui->textEdit->setText("串口关闭失败\n");
        connect(m_serial,&QSerialPort::readyRead,this,&client3::messlot);
    }
    else
    {
        ui->textEdit->setText("串口已关闭\n");
    }
    ui->pushButton_14->setEnabled(true);
    ui->pushButton_13->setEnabled(false);
}
client3::~client3()
{
    delete ui;
}
void client3::on_pushButton_25_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_16->setPixmap(img);
        ui->label_16->setScaledContents(true);
        ui->pushButton_25->setText(tr("关"));
        QString msg = "厨房灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_16->setPixmap(omg);
        ui->label_16->setScaledContents(true);
        ui->pushButton_25->setText(tr("开"));
        QString msg = "厨房灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }
}

void client3::on_pushButton_38_clicked()
{
    QPixmap img=QPixmap("G:/picture/5.jpeg");
    QPixmap omg=QPixmap("G:/picture/4.jpeg");
    static int a=1;
    if(a==1)
    {
        ui->label_33->setPixmap(img);
        ui->label_33->setScaledContents(true);
        ui->pushButton_38->setText(tr("关"));
        QString msg = "厨房风扇打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_33->setPixmap(omg);
        ui->label_33->setScaledContents(true);
        ui->pushButton_38->setText(tr("开"));
        QString msg = "厨房风扇关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_51_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_17->setPixmap(img);
        ui->label_17->setScaledContents(true);
        ui->pushButton_51->setText(tr("关"));
        QString msg = "餐厅灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_17->setPixmap(omg);
        ui->label_17->setScaledContents(true);
        ui->pushButton_51->setText(tr("开"));
        QString msg = "餐厅灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_81_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_34->setPixmap(img);
        ui->label_34->setScaledContents(true);
        ui->pushButton_81->setText(tr("关"));
        QString msg = "卫生间灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_34->setPixmap(omg);
        ui->label_34->setScaledContents(true);
        ui->pushButton_81->setText(tr("开"));
        QString msg = "卫生间灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_82_clicked()
{
    QPixmap img=QPixmap("G:/picture/5.jpeg");
    QPixmap omg=QPixmap("G:/picture/4.jpeg");
    static int a=1;
    if(a==1)
    {
        ui->label_35->setPixmap(img);
        ui->label_35->setScaledContents(true);
        ui->pushButton_82->setText(tr("关"));
        QString msg = "卫生间风扇打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_35->setPixmap(omg);
        ui->label_35->setScaledContents(true);
        ui->pushButton_82->setText(tr("开"));
        QString msg = "卫生间风扇关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_24_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_38->setPixmap(img);
        ui->label_38->setScaledContents(true);
        ui->pushButton_24->setText(tr("关"));
        QString msg = "次卧1灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_38->setPixmap(omg);
        ui->label_38->setScaledContents(true);
        ui->pushButton_24->setText(tr("开"));
        QString msg = "次卧1灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_83_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_40->setPixmap(img);
        ui->label_40->setScaledContents(true);
        ui->pushButton_83->setText(tr("关"));
        QString msg = "次卧2灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_40->setPixmap(omg);
        ui->label_40->setScaledContents(true);
        ui->pushButton_83->setText(tr("开"));
        QString msg = "次卧2灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_53_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_18->setPixmap(img);
        ui->label_18->setScaledContents(true);
        ui->pushButton_53->setText(tr("关"));
        QString msg = "书房灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_18->setPixmap(omg);
        ui->label_18->setScaledContents(true);
        ui->pushButton_53->setText(tr("开"));
        QString msg = "书房灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }
}

void client3::on_pushButton_54_clicked()
{
    QPixmap img=QPixmap("G:/picture/5.jpeg");
    QPixmap omg=QPixmap("G:/picture/4.jpeg");
    static int a=1;
    if(a==1)
    {
        ui->label_19->setPixmap(img);
        ui->label_19->setScaledContents(true);
        ui->pushButton_54->setText(tr("关"));
        QString msg = "卫生间风扇打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_19->setPixmap(omg);
        ui->label_19->setScaledContents(true);
        ui->pushButton_54->setText(tr("开"));
        QString msg = "卫生间风扇关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_84_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_42->setPixmap(img);
        ui->label_42->setScaledContents(true);
        ui->pushButton_84->setText(tr("关"));
        QString msg = "客厅灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_42->setPixmap(omg);
        ui->label_42->setScaledContents(true);
        ui->pushButton_84->setText(tr("开"));
        QString msg = "客厅灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_85_clicked()
{
    QPixmap img=QPixmap("G:/picture/5.jpeg");
    QPixmap omg=QPixmap("G:/picture/4.jpeg");
    static int a=1;
    if(a==1)
    {
        ui->label_43->setPixmap(img);
        ui->label_43->setScaledContents(true);
        ui->pushButton_85->setText(tr("关"));
        QString msg = "客厅风扇打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_43->setPixmap(omg);
        ui->label_43->setScaledContents(true);
        ui->pushButton_85->setText(tr("开"));
        QString msg = "客厅风扇关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_86_clicked()
{
    QPixmap img=QPixmap("G:/picture/2.png");
    QPixmap omg=QPixmap("G:/picture/1.png");
    static int a=1;
    if(a==1)
    {
        ui->label_47->setPixmap(img);
        ui->label_47->setScaledContents(true);
        ui->pushButton_86->setText(tr("关"));
        QString msg = "主卧灯打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_47->setPixmap(omg);
        ui->label_47->setScaledContents(true);
        ui->pushButton_86->setText(tr("开"));
        QString msg = "主卧灯关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_87_clicked()
{
    QPixmap img=QPixmap("G:/picture/5.jpeg");
    QPixmap omg=QPixmap("G:/picture/4.jpeg");
    static int a=1;
    if(a==1)
    {
        ui->label_48->setPixmap(img);
        ui->label_48->setScaledContents(true);
        ui->pushButton_87->setText(tr("关"));
        QString msg = "主卧风扇打开";
        m_serial->write(msg.toUtf8());
        a=0;
    }
    else
    {
        ui->label_48->setPixmap(omg);
        ui->label_48->setScaledContents(true);
        ui->pushButton_87->setText(tr("开"));
        QString msg = "主卧风扇关闭";
        m_serial->write(msg.toUtf8());
        a=1;
    }

}

void client3::on_pushButton_996_clicked()
{
    client4 *p = new client4;
    p->show();
    hide();
}

void client3::on_pushButton_997_clicked()
{
    client5 *p = new client5;
    p->show();
    hide();
}

void client3::on_pushButton_998_clicked()
{
    client6 *p = new client6;
    p->show();
    hide();
}
//发送请求给ai
void client3::on_pushButton_clicked()
{
    //创建一个网络访问管理对象
    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl(QString("http://api.qingyunke.com/api.php?key=free&appid=0&msg=%1").arg(ui->lineEdit->text()))));
    ui->lineEdit->clear();

}
//ai返回信息处理
void client3::replyFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError) {
        // 读取返回的数据
        QByteArray responseData = reply->readAll();
        // 将数据转换为字符串
        QString responseStr(responseData);
        // 解析JSON数据
        QJsonParseError parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(responseStr.toUtf8(), &parseError);
        if (parseError.error == QJsonParseError::NoError) {
            if (jsonDoc.isObject()) {
                QJsonObject jsonObj = jsonDoc.object();
                if (jsonObj.contains("content")) {
                    // 获取content字段的值
                    QString content = jsonObj["content"].toString();
                    // 显示content字段的值
                    ui->textEdit_2->setText(content);
                }
            }
        } else {
            qDebug() << "JSON parsing error:" << parseError.errorString();
        }
    } else {
        // 处理错误情况
        qDebug() << "Error:" << reply->errorString();
    }
    // 删除网络访问管理对象
    delete manager;
}

